package com.example.demo_2301a.wed;

import org.springframework.boot.autoconfigure.security.SecurityProperties;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author LR
 * @version 1.0
 * @date 2025/2/26--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+++++++++
 *
 * @description TODO
 */
public class UserDao {
    public USer getUserByName(String name) throws Exception {
        Connection conn = JDBCUntl.getConnection();
        String sql="select * from user where name=?";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setString(1,name);
        ResultSet re = pre.executeQuery();
        USer uSer=null;
        while (re.next()){
            int id = re.getInt(1);
            String name2 = re.getString(2);
            String password = re.getString(3);
            int status = re.getInt(4);
            uSer = new USer(id, name2, password, status);
        }
        JDBCUntl.Close(conn,pre,re);

        return uSer;
    }
    //添加
    public int addUser(String name,String password) throws Exception {
        Connection conn = JDBCUntl.getConnection();
        String sql ="insert into user(name,password,status) values(?,?,1)";
        PreparedStatement pstm = conn.prepareStatement(sql);
        pstm.setString(1, name);
        pstm.setString(2, password);
        int i = pstm.executeUpdate();
        JDBCUntl.Close(conn, pstm, null);
        return i;
    }
}
